//
//  JGBaseViewController.m
//  JGPicGridDemo
//
//  Created by mac on 15/10/29.
//  Copyright (c) 2015年 Free. All rights reserved.
//

#import "JGBaseViewController.h"
#import "UIView+Category.h"
#import "NSDictionary+Category.h"
#import "UIButton+Category.h"
#import "JGNavButton.h"

#define RGB(r,g,b) RGBA(r,g,b,1)

#define RGBA(r,g,b,a) [UIColor \
colorWithRed:r/255.0 \
green:g/255.0 \
blue:b/255.0 alpha:a]

@interface JGBaseViewController ()

@end

@implementation JGBaseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = RGB(240.0, 239.0, 244.0);
    
    //设置导航栏主题
    [self setupNavStyle];
    
    //设置导航栏按钮
    [self setUpNavButton];
}

/**
 * 设置导航栏
 */
-(void)setupNavStyle
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg128.png"] forBarMetrics:UIBarMetricsDefault];
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
    else
    {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];
    }
    
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.hidesBottomBarWhenPushed = YES;
    NSDictionary *dic = @{UITextAttributeFont: [UIFont boldSystemFontOfSize:20.0f],UITextAttributeTextColor:[UIColor whiteColor], UITextAttributeTextShadowColor:[UIColor clearColor]};
    self.navigationController.navigationBar.titleTextAttributes = dic;
}

/**
 * 设置导航栏按钮
 */
-(void)setUpNavButton
{
    _rightNavButton = [self p_rightBarButton];
    if (_rightNavButton)
    {
        [_rightNavButton addTarget:self action:@selector(rightNavButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem =  [[UIBarButtonItem alloc] initWithCustomView:_rightNavButton];
    }
    
    _leftNavButton = [self leftBarButton];
    if (_leftNavButton)
    {
        [_leftNavButton addTarget:self action:@selector(leftNavButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_leftNavButton];
        [self.navigationItem.leftBarButtonItem setTitle:@""];
    }
}

/**
 * 设置导航栏右边按钮
 */
- (JGNavButton *)p_rightBarButton
{
    if (_rightNavButtonPraDic && _rightNavButtonPraDic.count > 0)
    {
        JGNavButton *button = [JGNavButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(self.view.width -  50 , 5, 50, 44);
        //设置标题
        if ([_rightNavButtonPraDic objectOrNilForKey:@"Title"])
        {
            button.normalTitle = [_rightNavButtonPraDic objectOrNilForKey:@"Title"];
            button.titleLabel.font = [UIFont systemFontOfSize:15.0f];
            button.normalTitleColor = [UIColor whiteColor];
            button.highlightedTitleColor = RGB(34.0, 86.0, 150.0f);
            
            if ([button.normalTitle length] >= 4)
            {
                button.left -= 15;
                button.width += 15;
            }
        }
        
        //设置图片
        if ([_rightNavButtonPraDic objectOrNilForKey:@"NormalImage"])
        {
            button.normalImage = [UIImage imageNamed:[_rightNavButtonPraDic objectOrNilForKey:@"NormalImage"]];
            button.highlightedImage = [UIImage imageNamed:[_rightNavButtonPraDic objectOrNilForKey:@"HighlightImage"]];
        }
        
        return button;
    } else {
        return nil;
    }
}

/**
 * 设置导航栏右边按钮点击事件
 */
- (void)rightNavButtonClicked:(JGNavButton *)button
{
}

/**
 * 设置导航栏左边按钮
 */
- (JGNavButton *)leftBarButton
{
    if (self.navigationController.viewControllers.count > 1)
    {
        JGNavButton *button = [JGNavButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 5, 45, 44);
        button.normalImage = [UIImage imageNamed:@"nav_icon_back.png"];
        button.highlightedImage = [UIImage imageNamed:@"nav_icon_back_press.png"];
        
        return button;
    } else
        return nil;
}

/**
 * 设置导航栏左边按钮点击事件
 */
- (void)leftNavButtonClicked:(JGNavButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    [self.view endEditing:YES];
}

@end
